﻿// Nail.ImageConverter Library
// Nail framework
//
// Copyright © Frank Nagl, 2008
// admin@franknagl.de
// www.franknagl.de
//
namespace Nail
{
    using System;
    using System.Runtime.InteropServices;
    using System.Diagnostics;

    /// <summary>
    /// Provides static methods to reduce the used memory.
    /// </summary>
    public static class MemoryReducer
    {
        /// <summary>
        /// Reduces the memory usage.
        /// </summary>
        public static void ReduceMemoryUsage()
        {
            try
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
            }
            catch { }
        }

        [DllImport("kernel32.dll")]
        private static extern bool SetProcessWorkingSetSize(IntPtr hProcess,
        int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);
    }
}
